package com.flame.shapeville.component.bonus1;

import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.web.WebView;

/**
 * Represents Shape 4 from Bonus Task 1.
 * Displays a right-angled trapezoid with labeled dimensions and formula explanation.
 *
 * @author Flame Dev Team
 * @version 1.0
 * @since 2025-05-19
 */
public class Shape4 extends JfShape {

    /**
     * Default constructor.
     * Initializes the shape and loads both visual and detail components.
     */
    public Shape4() {
        super();
        loadShape();
        loadDetails();
    }

    /**
     * Constructor with custom size.
     * Sets the size and recalculates center coordinates before loading the shape.
     *
     * @param size the desired size of the shape
     */
    public Shape4(double size) {
        this.size = size;
        centerX = size / 2;
        centerY = size / 2;
        loadShape();
    }

    /**
     * Loads the visual representation of Shape 4.
     */
    public void loadShape() {
        double multi = size / 6;
        graphics = new Polygon(
                centerX - 2 * multi, centerY - 3 * multi,
                centerX - 2 * multi, centerY + 3 * multi,
                centerX + 2 * multi, centerY + 3 * multi,
                centerX + 2 * multi, centerY);
        graphics.setFill(Color.CYAN);
        graphics.setStroke(Color.GRAY);
        graphics.setStrokeWidth(2);
        shape.getChildren().add(graphics);

        Label label1 = new Label("6 m");
        label1.setLayoutX(centerX - 2 * multi - 30);
        label1.setLayoutY(centerY);

        Label label2 = new Label("4 m");
        label2.setLayoutX(centerX);
        label2.setLayoutY(centerY + 3 * multi + 5);

        Label label3 = new Label("3 m");
        label3.setLayoutX(centerX + 2 * multi + 5);
        label3.setLayoutY(centerY + 1 * multi);

        Label label4 = new Label("5 m");
        label4.setLayoutX(centerX + 5);
        label4.setLayoutY(centerY - 2 * multi);

        shape.getChildren().addAll(label1, label2, label3, label4);
    }

    /**
     * Loads the detailed view of Shape 4, including a horizontal dashed line and a web-based formula explanation.
     */
    public void loadDetails() {
        double multi = size / 6;
        graphics = new Polygon(
                centerX - 2 * multi, centerY - 3 * multi,
                centerX - 2 * multi, centerY + 3 * multi,
                centerX + 2 * multi, centerY + 3 * multi,
                centerX + 2 * multi, centerY);
        graphics.setFill(Color.CYAN);
        graphics.setStroke(Color.GRAY);
        graphics.setStrokeWidth(2);
        details.getChildren().add(graphics);

        Label label1 = new Label("6 m");
        label1.setLayoutX(centerX - 2 * multi - 30);
        label1.setLayoutY(centerY);

        Label label2 = new Label("4 m");
        label2.setLayoutX(centerX);
        label2.setLayoutY(centerY + 3 * multi + 5);

        Label label3 = new Label("3 m");
        label3.setLayoutX(centerX + 2 * multi + 5);
        label3.setLayoutY(centerY + 1 * multi);

        Label label4 = new Label("5 m");
        label4.setLayoutX(centerX + 5);
        label4.setLayoutY(centerY - 2 * multi);

        details.getChildren().addAll(label1, label2, label3, label4);

        Line splitLine = new Line();
        splitLine.setStartX(centerX - 2 * multi);
        splitLine.setStartY(centerY);
        splitLine.setEndX(centerX + 2 * multi);
        splitLine.setEndY(centerY);
        splitLine.setStroke(Color.ORANGE);
        splitLine.setStrokeWidth(2);
        splitLine.getStrokeDashArray().addAll(10d, 4d);

        Line dashedLine = new Line();
        dashedLine.setStartX(0);
        dashedLine.setStartY(20);
        dashedLine.setEndX(0);
        dashedLine.setEndY(220);
        dashedLine.setStroke(Color.GRAY);
        dashedLine.setStrokeWidth(2);
        dashedLine.getStrokeDashArray().addAll(10d, 4d);
        dashedLine.setTranslateX(239);

        WebView webView = new WebView();
        webView.setPrefSize(220, 240);
        webView.setLayoutX(241);
        webView.setStyle("-fx-background-color: transparent;");
        String url = getClass().getResource("/static/formula/bonus1/shape4.html").toExternalForm();
        webView.getEngine().load(url);

        details.getChildren().addAll(splitLine, dashedLine, webView);
    }
}
